home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lynx-2.4 / WWW / Library / Implementation / HTAuth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-28  |  5.6 KB  |  211 lines

  1.  
  2. /* MODULE                            HTAuth.c
  3. **            USER AUTHENTICATION
  4. **
  5. ** AUTHORS:
  6. **    AL    Ari Luotonen    luotonen@dxcern.cern.ch
  7. **
  8. ** HISTORY:
  9. **    AL 14.10.93 Fixed the colon-not-allowed-in-password-bug.
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16. #include "HTUtils.h"
  17. #include <string.h>
  18. #include "HTPasswd.h"    /* Password file routines    */
  19. #include "HTAssoc.h"
  20. #include "HTAuth.h"    /* Implemented here        */
  21. #include "HTUU.h"    /* Uuencoding and uudecoding    */
  22.  
  23. #include "LYLeaks.h"
  24.  
  25. /* PRIVATE                        decompose_auth_string()
  26. **        DECOMPOSE AUTHENTICATION STRING
  27. **        FOR BASIC OR PUBKEY SCHEME
  28. ** ON ENTRY:
  29. **    authstring    is the authorization string received
  30. **            from browser.
  31. **
  32. ** ON EXIT:
  33. **    returns        a node representing the user information
  34. **            (as always, this is automatically freed
  35. **            by AA package).
  36. */
  37. PRIVATE HTAAUser *decompose_auth_string ARGS2(char *,        authstring,
  38.                           HTAAScheme,    scheme)
  39. {
  40.     static HTAAUser *user = NULL;
  41.     static char *cleartext = NULL;
  42.     char *username = NULL;
  43.     char *password = NULL;
  44.     char *inet_addr = NULL;
  45.     char *timestamp = NULL;
  46.     char *browsers_key = NULL;
  47.  
  48.     if (!user && !(user = (HTAAUser*)malloc(sizeof(HTAAUser))))    /* Allocated */
  49.     outofmem(__FILE__, "decompose_auth_string");        /* only once */
  50.  
  51.     user->scheme = scheme;
  52.     user->username = NULL;    /* Not freed, because freeing */
  53.     user->password = NULL;    /* cleartext also frees these */
  54.     user->inet_addr = NULL;    /* See below: ||              */
  55.     user->timestamp = NULL;    /*            ||              */
  56.     user->secret_key = NULL;    /*            ||              */
  57.                                 /*            \/              */
  58.     FREE(cleartext);    /* From previous call.                */
  59.                         /* NOTE: parts of this memory are pointed to by    */
  60.                         /* pointers in HTAAUser structure. Therefore,    */
  61.                         /* this also frees all the strings pointed to    */
  62.             /* by the static 'user'.            */
  63.  
  64.     if (!authstring || !*authstring || 
  65.     scheme != HTAA_BASIC || scheme == HTAA_PUBKEY)
  66.     return NULL;
  67.  
  68.     if (scheme == HTAA_PUBKEY) {    /* Decrypt authentication string */
  69.     int bytes_decoded;
  70.     char *ciphertext;
  71.     int len = strlen(authstring) + 1;
  72.  
  73.     if (!(ciphertext = (char*)malloc(len)) ||
  74.         !(cleartext  = (char*)malloc(len)))
  75.         outofmem(__FILE__, "decompose_auth_string");
  76.  
  77.     bytes_decoded = HTUU_decode(authstring,
  78.                     (unsigned char *)ciphertext, len);
  79.     ciphertext[bytes_decoded] = (char)0;
  80. #ifdef PUBKEY
  81.     HTPK_decrypt(ciphertext, cleartext, private_key);
  82. #endif
  83.     free(ciphertext);
  84.     }
  85.     else {   /* Just uudecode */
  86.     int bytes_decoded;
  87.     int len = strlen(authstring) + 1;
  88.     
  89.     if (!(cleartext = (char*)malloc(len)))
  90.         outofmem(__FILE__, "decompose_auth_string");
  91.     bytes_decoded = HTUU_decode(authstring,
  92.                     (unsigned char *)cleartext, len);
  93.     cleartext[bytes_decoded] = (char)0;
  94.     }
  95.  
  96.  
  97. /*
  98. ** Extract username and password (for both schemes)
  99. */
  100.     username = cleartext;
  101.     if (!(password = strchr(cleartext, ':'))) {
  102.     if (TRACE)
  103.         fprintf(stderr, "%s %s\n",
  104.             "decompose_auth_string: password field",
  105.             "missing in authentication string.\n");
  106.     return NULL;
  107.     }
  108.     *(password++) = '\0';
  109.  
  110. /*
  111. ** Extract rest of the fields
  112. */
  113.     if (scheme == HTAA_PUBKEY) {
  114.     if (                          !(inet_addr   =strchr(password, ':')) || 
  115.         (*(inet_addr++)   ='\0'), !(timestamp   =strchr(inet_addr,':')) ||
  116.         (*(timestamp++)   ='\0'), !(browsers_key=strchr(timestamp,':')) ||
  117.         (*(browsers_key++)='\0')) {
  118.  
  119.         if (TRACE) fprintf(stderr, "%s %s\n",
  120.                    "decompose_auth_string: Pubkey scheme",
  121.                    "fields missing in authentication string");
  122.         return NULL;
  123.     }
  124.     }
  125.  
  126. /*
  127. ** Set the fields into the result
  128. */
  129.     user->username   = username;
  130.     user->password   = password;
  131.     user->inet_addr  = inet_addr;
  132.     user->timestamp  = timestamp;
  133.     user->secret_key = browsers_key;
  134.  
  135.     if (TRACE) {
  136.     if (scheme==HTAA_BASIC)
  137.         fprintf(stderr, "decompose_auth_string: %s (%s,%s)\n",
  138.             "Basic scheme authentication string:",
  139.             username, password);
  140.     else
  141.         fprintf(stderr, "decompose_auth_string: %s (%s,%s,%s,%s,%s)\n",
  142.             "Pubkey scheme authentication string:",
  143.             username, password, inet_addr, timestamp, browsers_key);
  144.     }
  145.     
  146.     return user;
  147. }
  148.  
  149.  
  150.  
  151. PRIVATE BOOL HTAA_checkTimeStamp ARGS1(CONST char *, timestamp)
  152. {
  153.     return NO;        /* This is just a stub */
  154. }
  155.  
  156.  
  157. PRIVATE BOOL HTAA_checkInetAddress ARGS1(CONST char *, inet_addr)
  158. {
  159.     return NO;        /* This is just a stub */
  160. }
  161.  
  162.  
  163. /* SERVER PUBLIC                    HTAA_authenticate()
  164. **            AUTHENTICATE USER
  165. ** ON ENTRY:
  166. **    scheme        used authentication scheme.
  167. **    scheme_specifics the scheme specific parameters
  168. **            (authentication string for Basic and
  169. **            Pubkey schemes).
  170. **    prot        is the protection information structure
  171. **            for the file.
  172. **
  173. ** ON EXIT:
  174. **    returns        NULL, if authentication failed.
  175. **            Otherwise a pointer to a structure
  176. **            representing authenticated user,
  177. **            which should not be freed.
  178. */
  179. PUBLIC HTAAUser *HTAA_authenticate ARGS3(HTAAScheme,    scheme,
  180.                      char *,    scheme_specifics,
  181.                      HTAAProt *,    prot)
  182. {
  183.     if (HTAA_UNKNOWN == scheme || !prot ||
  184.     -1 == HTList_indexOf(prot->valid_schemes, (void*)scheme))
  185.     return NULL;
  186.  
  187.     switch (scheme) {
  188.       case HTAA_BASIC:
  189.       case HTAA_PUBKEY:
  190.     {
  191.         HTAAUser *user = decompose_auth_string(scheme_specifics, scheme);
  192.                                        /* Remember, user is auto-freed */
  193.         if (user &&
  194.         HTAA_checkPassword(user->username,
  195.                    user->password,
  196.                    HTAssocList_lookup(prot->values, "passw")) &&
  197.         (HTAA_BASIC == scheme ||
  198.          (HTAA_checkTimeStamp(user->timestamp) &&
  199.           HTAA_checkInetAddress(user->inet_addr))))
  200.         return user;
  201.         else
  202.         return NULL;
  203.     }
  204.     break;
  205.       default:
  206.     /* Other authentication routines go here */
  207.     return NULL;
  208.     }
  209. }
  210.  
  211.